package com.project_test.kun.travelling_1st.HomePage.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.project_test.kun.travelling_1st.Discovery.Discover_Detail_Activity;
import com.project_test.kun.travelling_1st.HomePage.Util.URL;
import com.project_test.kun.travelling_1st.HomePage.activities.HomePageDetilActivity;
import com.project_test.kun.travelling_1st.HomePage.category.HomePListItemIndex;
import com.project_test.kun.travelling_1st.R;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 16-10-18.
 */
public class HomePAdapter extends BaseAdapter {
    private Context mContext;
    private List<HomePListItemIndex> data;
    private HomePListItemIndex mIndex;
    private ViewHolder mHolder;
    private HomePListItemIndex mIndex1;


    public HomePAdapter(Context context, List<HomePListItemIndex> data) {
        mContext = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data!=null?data.size():0;
    }

    @Override
    public HomePListItemIndex getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        mHolder = null;
        if(view==null){
        view= LayoutInflater.from(mContext).inflate(R.layout.item_fragment_home_list,null);
            mHolder =new ViewHolder();
            mHolder.more= (TextView) view.findViewById(R.id.item_more__fragment_home);
            mHolder.title= (TextView) view.findViewById(R.id.item_title_fragment_home);
            mHolder.pic_big= (ImageView) view.findViewById(R.id.item_pic_big_fragment_home);
            mHolder.pic_s1= (ImageView) view.findViewById(R.id.item_pic_s1_fragment_home);
            mHolder.pic_s2= (ImageView) view.findViewById(R.id.item_pic_s2_fragment_home);
            view.setTag(mHolder);
        }else{
            mHolder = (ViewHolder) view.getTag();
        }
        //赋值
        mIndex1 = data.get(i);
        mHolder.title.setText(mIndex1.getTitle());
        Picasso.with(mContext).load(mIndex1.getPic_big()).into(mHolder.pic_big);
        Picasso.with(mContext).load(mIndex1.getPic_s1()).into(mHolder.pic_s1);
        Picasso.with(mContext).load(mIndex1.getPic_s2()).into(mHolder.pic_s2);

        final int currentPosition= i;
        setListener(currentPosition);
        return view;
    }

    public void setListener(final int listener) {
        mHolder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, HomePageDetilActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("index", data.get(listener));
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
        mHolder.pic_big.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,Discover_Detail_Activity.class);
                String detailPath= URL.DETAIL_PATH_1+data.get(listener).getPic_big_id()+URL.DETAIL_PATH_2;
                intent.putExtra("detailPath",detailPath);
                mContext.startActivity(intent);
            }
        });
        mHolder.pic_s1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,Discover_Detail_Activity.class);
                String detailPath= URL.DETAIL_PATH_1+data.get(listener).getPic_s1_id()+URL.DETAIL_PATH_2;
                intent.putExtra("detailPath",detailPath);
                mContext.startActivity(intent);
            }
        });
        mHolder.pic_s2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,Discover_Detail_Activity.class);
                String detailPath= URL.DETAIL_PATH_1+data.get(listener).getPic_s2_id()+URL.DETAIL_PATH_2;
                intent.putExtra("detailPath",detailPath);
                mContext.startActivity(intent);
            }
        });
    }

    static class ViewHolder{
      public   ImageView pic_big,pic_s1,pic_s2;
       public TextView title,more;

    }
}
